package com.jk.service;

import com.alibaba.fastjson.JSONObject;
import com.jk.entity.*;
import com.jk.mapper.BalanceMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service("balanceService")
public class BalanceServiceImpl implements BalanceService{

/*
    public static final LeeLog LEE_LOG = new LeeLog();
*/
    @Resource
    private BalanceMapper balanceMapper;

  /*  @Resource
     private RabbitUtil rabbitUtil;*/

    @Override
    public List<Balance> querybalance(Balance balance) {

  /*      LeeLog leeLog = new LeeLog();

        leeLog.setLogid(new Random().nextInt(100));

        leeLog.setContent("查询积分");

        leeLog.setType("查询");

        leeLog.setSys("这是日志来源");

        leeLog.setAddtime(new Date());

       try {

            rabbitUtil.sendrabbit(leeLog);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }*/

        return balanceMapper.querybalance(balance);
    }


    @Override
    public List<Userinfo> queryuserinfo(Userinfo userinfo) {

    /*  ,String offset, String limit
    long  ShopCarSum  = balanceMapper.getuserinfosum(Integer.parseInt(offset),Integer.parseInt(limit));

        List<ShopCar>  ShopCarList  = balanceMapper.getuserinfolist(Integer.parseInt(offset),Integer.parseInt(limit));

        JSONObject  json  =  new JSONObject();

        json.put("total", ShopCarSum);

        json.put("rows", ShopCarList);*/

        return balanceMapper.queryuserinfo(userinfo);
    }

    @Override
    public Userinfo querycardinfo(Userinfo userinfo) {

        System.out.println(userinfo);

        Userinfo userif = balanceMapper.querycardinfo(userinfo.getUserid());

        userif.setCardnumberlist(balanceMapper.querybankcardnumber(userinfo.getUserid()));

        return userif;

    }

    @Override
    public Message queryintegral(Integral integral) {

        Integral integral1 = balanceMapper.queryintegral(integral.getUserid());

        int lastyearintegral = integral1.getLastyearintegral();

        int newyearintegral = integral1.getNewyearintegral();

        System.out.println("去年"+lastyearintegral+"今年"+newyearintegral);

        Message message = new Message();

        if (newyearintegral>lastyearintegral){

            int grade = (int) Math.floor(newyearintegral/3000);

            message.setMid(grade);

            message.setMessage("vip"+grade);

            integral.setUservip("vip"+grade);

            balanceMapper.updateintegral(integral);

            return message;
        }
        if (newyearintegral<lastyearintegral){

            int grade = (int) Math.floor(lastyearintegral/3000);

            System.out.println(grade+"ggggggggggg");
            grade =  grade-1;

            message.setMid(grade);

            message.setMessage("vip"+grade);

            integral.setUservip("vip"+grade);

            balanceMapper.updateintegral(integral);

            return message;

        }

        return message;
    }

    @Override
    public Message saverecharge(Recharge recharge) {

        Message message = new Message();

        recharge.setIdcard("130182199411222442");

        String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
                "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

        boolean matches = recharge.getIdcard().matches(regularExpression);

//判断传过来的身份呢
        if (matches) {

            if (recharge.getRechargenum() <= 20000 && recharge.getRechargenum() >= 0) {

                System.out.println("充值成功");

                balanceMapper.saverecharge(recharge);

                message.setMid(100);

                message.setMessage("成功");

                return message;
            }

            if (recharge.getRechargenum() > 20000) {

                message.setMid(01);
                message.setMessage("单笔消费不能超过2万元");

                return message;

            }
            if(recharge.getRechargenum()<0){

                message.setMid(03);
                message.setMessage("输入的金额不正确");

            }


        }else {

            message.setMid(99);
            message.setMessage("身份证格式不正确");

            return message;
        }
        return message;
    }

    @Override
    public void addcard(BankInfo bankInfo) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        bankInfo.setBankcardid(uuid);
        bankInfo.setCardid("6abeef823d7511e8");
        balanceMapper.addcard(bankInfo);

    }
}
